
import axios from 'axios'

// 解决每次ajax请求，对应的sessionId不一致的问题
axios.defaults.withCredentials = true;
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
// 创建axios实例
const service = axios.create({
  // axios中请求配置有baseURL选项，表示请求URL公共部分
  baseURL: process.env.VUE_APP_SERVER,
  // 超时
  timeout: 10000
})
/**
 * axios拦截器
 */
 service.interceptors.request.use(function (config) {
  config.headers['Authorization'] = 'Bearer ' + (SessionStorage.get("user_token") || '') // 让每个请求携带自定义token 请根据实际情况自行修改
  return config;
}, error => {});
service.interceptors.response.use(function (response) {
  if(response.data.status === 401){
    Toast.warning("请登录");
    location.href = "/login?redirect=" + encodeURIComponent(window.location.href)
  }
  return response;
}, error => {});

export default service